package com.firesrc.mail.send;

import java.util.ArrayList;
import java.util.List;

import com.firesrc.bean.MailHost;
import com.firesrc.service.MailHostService;
import com.firesrc.service.impl.MailHostServiceImpl;

public class MailSendManagerFactory {

	private List<MailSender> list = new ArrayList<MailSender>();

	public void initFromDb() {
		MailHostService mailHostService = new MailHostServiceImpl();
		List<MailHost> allList = mailHostService.getAllCurrectList();
		for (MailHost mailHost : allList) {
			list.add(getMailSenderFromMailHost(mailHost));
		}
	}

	public void init() {
		initFromDb();
		//		// list.add(new MailSendQQManager("523560765", "520211paiming"));
		//		list.add(new MailSendGmailManager("baidupaimin", "baidupaimin")); ok
		//		// list.add(new MailSend163Manager("samlinzhang", "hibernate"));
		//		list.add(new MailSendSinaManager("zympw@sina.com", "520211jiayou2012"));
		//		list.add(new MailSend21CnManager("bdpm123", "hibernate"));  ok
		//
		//		// list.add(new MailSendQQManager("523560765", "520211paiming"));
		//		// list.add(new MailSendGmailManager("minyuejun2012", "myj830325"));
		//		// list.add(new MailSend163Manager("zypmw003", "520211jiayou2012"));
		//		list.add(new MailSendSinaManager("baidupaimin@sina.com", "hibernate"));
		//		list.add(new MailSend21CnManager("hkdjxl", "format123"));
		//
		//		list.add(new MailSendQQManager("1547074699", "520211paiming"));
		//		// list.add(new MailSendQQManager("441918580", "520211jiayou"));
		//		list.add(new MailSendQQManager("954691110", "hibernate33"));
		//		list.add(new MailSendGmailManager("zypmw001", "19830211"));
		//
		//		list.add(new MailSendJuRanManager("jobjp", "jobjp"));// fixme qq入垃圾箱, gmail hotmail 收不到
		//		list.add(new MailSendGmailManager("lxit.xll", "hibernate"));
		//		list.add(new MailSendGmailManager("lxitproject", "hibernate3388"));

		// list.add(new MailSendSinaManager("zypmw001@sina.com", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw002@sina.com", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw003@sina.com", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw004@sina.com", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw005@sina.com", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw006@sina.com", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw007@sina.com", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw008@sina.com", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw009@sina.com", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw001@sina.cn", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw002@sina.cn", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw003@sina.cn", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw004@sina.cn", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw005@sina.cn", "520211jiayou2012"));
		//		list.add(new MailSendSinaManager("zypmw006@sina.cn", "520211jiayou2012"));
		//		list.add(new MailSendSinaManager("zypmw007@sina.cn", "520211jiayou2012"));
		//		list.add(new MailSendSinaManager("zypmw008@sina.cn", "520211jiayou2012"));
		// list.add(new MailSendSinaManager("zypmw009@sina.cn", "520211jiayou2012"));
	}

	public static MailSender getMailSenderFromMailHost(MailHost mailHost) {
		MailSender mailSender = new MailSender(mailHost.getUserName(), mailHost.getPassword());
		mailSender.setHostName(mailHost.getHostName());
		mailSender.setMail(mailHost.getMail());
		mailSender.setSsl(mailHost.isSsl());
		mailSender.setMaxDayCount(mailHost.getSendDayMaxCount());
		mailSender.setMaxOnceCount(mailHost.getSendOnceMaxCount());
		mailSender.setKind(mailHost.getKind());
		mailSender.setAlreadyDayCount(mailHost.getDayAlreadySendCount());
		return mailSender;
	}

	public synchronized MailSender getMailSendManager(int i) {
		if (list.size() == 0) {
			init();
		}
		int value = i % list.size();
		MailSender superMailSender = list.get(value);
		System.out.println("得到发邮件服务器:" + superMailSender.getMail());
		return superMailSender;

	}

	public List<MailSender> getList() {
		if (list.size() == 0) {
			init();
		}
		return list;
	}

}
